package com.mycompany.inventory.viewmodel;

import java.util.ArrayList;
import java.util.List;

import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.event.Event;

import com.mycompany.inventory.persistence.ProductDAO;
import com.mycompany.inventory.persistence.StorageInfoDAO;
import com.mycompany.inventory.persistence.VendorDAO;
import com.mycompany.inventory.pojo.Product;
import com.mycompany.inventory.pojo.StorageInfo;
import com.mycompany.inventory.pojo.Vendor;
import com.mycompany.inventory.util.Constant;

public class StockViewModel implements org.zkoss.zk.ui.event.EventListener{
	private List<Product> products = new ArrayList<Product>();
	private ProductDAO pDao = new ProductDAO();
	private List<Vendor> lastVendor;
	StorageInfo storage = new StorageInfo();

	public void onEvent(Event evt) throws InterruptedException {
        if (evt.getName().equals("onOK")) {

        } else {
        	System.out.println("###Target: " + evt.getTarget()); 
        	return;
        }
    }

	/**
	 * @return the products
	 */
	public List<Product> getProducts() {
		return products;
	}

	/**
	 * @param products the products to set
	 */
	public void setProducts(List<Product> products) {
		this.products = products;
	}

	@Init
	public void init(){
		this.refeshCurrentStockGrid();
	}
	
	public void refeshCurrentStockGrid(){
		this.products = this.pDao.findByType(Constant.STOCKABLE_NUMBER);
		for(Product product : this.products){
			this.lastVendor = new VendorDAO().findLastVendor(Long.parseLong(product.getId()));
			if(this.lastVendor.size() != 0){
				product.setLastVendor(this.lastVendor.get(0).getName());
			}
			
			this.storage = new StorageInfoDAO().find(Long.parseLong(product.getId()));
			if(this.storage != null){
				product.setStorage(this.storage);
			}else{
				product.setStorage(new StorageInfo());
			}			
		}
		
	}
}